<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号码" prop="idno">
        <el-input
          v-model="queryParams.idno"
          placeholder="请输入身份证号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >重置</el-button>
      </el-form-item>
      </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['user:kfjz:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['user:kfjz:edit']"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['user:kfjz:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['user:kfjz:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>
    <el-table
      v-loading="loading"
      :data="kfjzList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="身份证号码" align="center" prop="idno" />
      <el-table-column label="性别" align="center" prop="gender">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.gender" :value="scope.row.gender" />
        </template>
      </el-table-column>
      <el-table-column label="民族" align="center" prop="mz" />
      <el-table-column
        label="出生日期"
        align="center"
        prop="birthday"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="婚姻状况" align="center" prop="hyzk">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hyzk" :value="scope.row.hyzk" />
        </template>
      </el-table-column>
      <el-table-column label="就业情况" align="center" prop="jyqk">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.jyqk" :value="scope.row.jyqk" />
        </template>
      </el-table-column>
      <el-table-column label="电话" align="center" prop="mobile" />
      <el-table-column label="入所日期" align="center" prop="rsrq" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rsrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出所日期" align="center" prop="csrq" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.csrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['user:kfjz:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['user:kfjz:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listUser, delUser } from '@/api/user/userInfo'

export default {
  name: 'Kfjz',
  dicts: ['hyzk', 'sys_yes_no', 'gender', 'jyqk'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 康复救助人员表格数据
      kfjzList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        subType: 'drug_kfjz',
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        idno: undefined,
        gender: undefined,
        mz: undefined,
        birthday: undefined,
        hyzk: undefined,
        jyqk: undefined,
        dh: undefined,
        rsrq: undefined,
        csrq: undefined,
        kfjzzx: undefined,
        szry: undefined,
        szdw: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [{ required: true, message: 'id不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        idno: [
          { required: true, message: '身份证号码不能为空', trigger: 'blur' }
        ],
        gender: [{ required: true, message: '性别不能为空', trigger: 'blur' }],
        mz: [{ required: true, message: '名族不能为空', trigger: 'blur' }],
        birthday: [
          { required: true, message: '出生日期不能为空', trigger: 'blur' }
        ],
        hyzk: [
          { required: true, message: '婚姻状况不能为空', trigger: 'blur' }
        ],
        jyqk: [
          { required: true, message: '就业情况不能为空', trigger: 'blur' }
        ],
        dh: [{ required: true, message: '电话不能为空', trigger: 'blur' }],
        rsrq: [
          { required: true, message: '入所日期不能为空', trigger: 'blur' }
        ],
        csrq: [
          { required: true, message: '出所日期不能为空', trigger: 'blur' }
        ],
        kfjzzx: [
          { required: true, message: '康复救助中心不能为空', trigger: 'blur' }
        ],
        szry: [
          { required: true, message: '送至人员不能为空', trigger: 'blur' }
        ],
        szdw: [
          { required: true, message: '送至单位不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询康复救助人员列表 */
    getList() {
      this.loading = true
      listUser(this.queryParams).then((response) => {
        this.kfjzList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        idno: undefined,
        gender: undefined,
        mz: undefined,
        birthday: undefined,
        hyzk: undefined,
        jyqk: undefined,
        dh: undefined,
        rsrq: undefined,
        csrq: undefined,
        kfjzzx: undefined,
        szry: undefined,
        szdw: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({ path: '/user/kfjz-detail/index' })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const kfjzId = row.id || this.ids[0]
      this.$router.push({
        path: '/user/kfjz-detail/index',
        query: { kfjzId: kfjzId }
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const kfjzIds = row.id || this.ids
      this.$modal
        .confirm('是否确认删除康复救助人员编号为"' + kfjzIds + '"的数据项？')
        .then(() => {
          this.loading = true
          return delUser(kfjzIds)
        })
        .then(() => {
          this.loading = false
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'user/kfjz/export',
        {
          ...this.queryParams
        },
        `kfjz_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
